﻿package effects
{
	import flash.display.BitmapData;
	import flash.display.Loader;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.external.ExternalInterface;
	import flash.geom.Matrix;
	import flash.net.URLRequest;
	
	public class Effect6 extends Effect
	{
		private var url:String = "assets/clouds0.png";
		private var loader:Loader = new Loader();
		private var matrix:Matrix;
		private var matrix1:Matrix;
		private var cloud0:BitmapData;
		private var cloud1:BitmapData;

		public function Effect6() 
		{
			cacheAsBitmap=true;
			var request:URLRequest = new URLRequest(url);
            
			loader.load(request);
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE, draw);
			loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
		}
		public override function add():void
	    {
	    	addEventListener(Event.ENTER_FRAME,render);
	    }
		public override function remove():void
		{
			removeEventListener(Event.ENTER_FRAME,render);
			var c:Number=Math.random()*2;
	    	matrix.rotate(Math.PI/c); 
	    	matrix.tx=0;
	    	matrix.ty=0;
	    	c=Math.random()*2;
	    	matrix1.rotate(Math.PI/c); 
	    	matrix1.tx=0;
	    	matrix1.ty=0;
		}
	    private function draw(event:Event):void 
		{
			cloud0 = new BitmapData(loader.width, loader.height, true);  
			cloud0.draw(loader, new Matrix());
	    
			cloud1 = cloud0.clone();
	    
			matrix = new Matrix();
			matrix.scale(1.4,1.4);
	    
			matrix1 = new Matrix();
			matrix1.a=1.6;
			matrix1.b=2.4;
			matrix1.scale(1.4,1.4);
	    	    
			with(graphics)
			{
				beginFill(0xdce6fe);
				drawRect(0,0,loader.width, loader.height);
				beginBitmapFill(cloud0, matrix, true,true);
		    
				drawRect(0, 0, Global._stage.stageWidth, Global._stage.stageHeight);
				endFill();
			}
			addEventListener(Event.ENTER_FRAME,render);
		}
	    private function render(e:Event):void
	    {
	    	
			matrix.tx+=4;
	    	matrix.ty+=2;
	    	matrix1.tx+=2;
	    	matrix1.ty+=1;
	    	with(graphics){
				clear();
				beginFill(0xdce6fe);
		    	drawRect(0,0,loader.width, loader.height);
			    beginBitmapFill(cloud0, matrix, true,true);
			    drawRect(0, 0, Global._stage.stageWidth, Global._stage.stageHeight);
			    endFill();
	     	}	    	
	    }
		private function ioErrorHandler(event:IOErrorEvent):void {
			ExternalInterface.call("alert","Unable to load image: " + url);
		}
	}
}